package com.order.config;

import org.bee.tl.ext.jfinal.BeetlRenderFactory;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.ext.plugin.shiro.ShiroInterceptor;
import com.jfinal.ext.plugin.shiro.ShiroPlugin;
import com.jfinal.ext.plugin.tablebind.AutoTableBindPlugin;
import com.jfinal.ext.plugin.tablebind.SimpleNameStyles;
import com.jfinal.ext.route.AutoBindRoutes;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.ViewType;
import com.order.interceptor.sys.LoginInterceptor;


/** 
* @ClassName: JFWebConfig 
* @Description: 项目主配置
* @author LinBo
* @date 2014年3月22日 下午4:00:11 
*  
*/ 
public class JFWebConfig extends JFinalConfig {
	/**
	 * 供Shiro插件使用。
	 */
	Routes routes;
	@Override
	public void configConstant(Constants me) {
		//开启开发模式
		me.setError401View("/common/401.htm");
		me.setError403View("/common/403.htm");
		me.setError404View("/common/err404.html");
		me.setError500View("/common/err500.html");
		me.setDevMode(true);
		me.setBaseViewPath(Consts.BASIC_VIEW_PATH);
		me.setViewType(ViewType.JSP);
		me.setMainRenderFactory(new BeetlRenderFactory());
	}

	@Override
	public void configRoute(Routes me) {
		this.routes = me;
		me.add(new AutoBindRoutes());
	}

	@Override
	public void configPlugin(Plugins me) {
		loadPropertyFile("classes/dataSource.properties");
		DruidPlugin druid = new DruidPlugin(getProperty("url"),
				getProperty("user"), 
				getProperty("password"),
				getProperty("driver"),
				getProperty("filters"));
		//用来检测连接是否有效的sql，要求是一个查询语句
		druid.setValidationQuery("select 1 from dual");
		druid.setMaxActive(Integer.parseInt(getProperty("maxActive")));
		druid.setPoolPreparedStatements(true);
		me.add(druid);
		
		//添加自动绑定model和表的插件
		AutoTableBindPlugin atbp = new AutoTableBindPlugin(druid, SimpleNameStyles.LOWER);
		atbp.setShowSql(true);
		atbp.setDialect(new MysqlDialect());
		//配置数据库字段名大小写不敏感的容器工厂
		atbp.setContainerFactory(new CaseInsensitiveContainerFactory());
		me.add(atbp);
		
		//配置shiro插件
		me.add(new ShiroPlugin(routes));
	}

	@Override
	public void configInterceptor(Interceptors me) {
		//session拦截器，用于调用session
		me.add(new SessionInViewInterceptor());
		//登录全局拦截器
		me.add(new LoginInterceptor());
		//Shiro全局拦截器
		me.add(new ShiroInterceptor());
		
	}

	@Override
	public void configHandler(Handlers me) {
		
	}

}
